<template>
  <div class="container">
    <!-- 顶部搜索区域 -->
    <el-card class="top-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <span>仓库编号</span>
          <el-input v-model="inputData.like_code" placeholder="请输入" />
        </el-col>
        <el-col :span="6">
          <span>仓库名称</span>
          <el-input v-model="inputData.like_name" placeholder="请输入" />
        </el-col>
        <el-col :span="6">
          <span>仓库状态</span>
          <el-select v-model="inputData.status" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in selector" :key="index" :value="item.value" :label="item.label" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button class="btn1" @click="searchFn">搜索</el-button>
          <el-button class="btn2" @click="resetFn">重置</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 底部卡片区域 -->
    <el-card>
      <el-row type="flex">
        <el-col :span="18">
          <el-button style="background-color: #00be76;border-radius: 50px;color: #fff;margin-bottom: 15px;" @click="addWareHouse">新增仓库</el-button>
        </el-col>
        <el-col :span="6">
          <el-button style="border-radius: 50px">下载库区模板</el-button>
          <el-button style="border-radius: 50px">导入库区信息</el-button>
        </el-col>
      </el-row>
      <!-- table表格区域 -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :header-cell-style="{textAlign: 'center'}"
      >
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center"
        />
        <el-table-column
          prop="code"
          label="仓库编号"
          width="150"
          align="center"
        />
        <el-table-column
          prop="name"
          label="仓库名称"
          width="150"
          align="center"
        />
        <el-table-column
          prop="type"
          label="仓库类型"
          width="150"
          align="center"
        >
          <template #default="scope">
            {{ typeMap[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="location"
          label="省/市/区"
          width="150"
          align="center"
        />
        <el-table-column
          prop="address"
          label="详细地址"
          width="150"
          align="center"
        />
        <el-table-column
          prop="status"
          label="仓库状态"
          width="150"
          align="center"
        >
          <template #default="scope">
            {{ statusMap[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="surface"
          label="仓库面积m²"
          width="150"
          align="center"
        />
        <el-table-column
          prop="includedNum"
          label="库区数量"
          width="150"
          align="center"
        />
        <el-table-column
          prop="personName"
          label="负责人"
          width="150"
          align="center"
        />
        <el-table-column
          prop="phone"
          label="联系电话"
          width="150"
          align="center"
        />
        <el-table-column
          prop="updateTime"
          label="更新时间"
          width="150"
          align="center"
        />
        <el-table-column
          label="操作"
          width="150"
          fixed="right"
          align="center"
        >
          <template #default="scope">
            <el-button type="text" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="switchBtn(scope.row)">{{ statusMap[Number(!scope.row.status)] }}</el-button>
            <el-button type="text" size="mini" @click="delBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页 -->
      <el-row type="flex" justify="center">
        <el-pagination
          :current-page="params.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="params.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="+total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>

    <!-- 弹窗 -->
    <el-dialog
      :title="'确认' + dialogTitle"
      :visible="dialogVisible"
      width="40%"
    >
      <span>确认要{{ dialogTitle }}库区： {{ dialogName }} 吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { pageQueryWareHouse, getNextCode, searchWareHouse, changeWareHouse, deleteWareHouse } from '@/api'
export default {
  data() {
    return {
      // 输入框数据
      inputData: {
        like_code: '',
        like_name: '',
        status: ''
      },
      // 选择器
      selector: [
        { value: 0, label: '停用' },
        { value: 1, label: '启用' },
        { value: null, label: '全部' }
      ],
      // 仓库列表数据
      tableData: [],
      params: {
        size: 10, // 每页显示几条
        current: 1 // 当前页
      },
      // 总条数
      total: 0,
      // 仓库状态 映射关系
      statusMap: {
        0: '停用',
        1: '启用'
      },
      // 仓库类型 映射关系
      typeMap: {
        'ZZ': '中转仓',
        'JG': '加工仓',
        'CB': '储备仓',
        'LC': '冷藏仓'
      },
      // 下一个编码
      nextCode: '',
      // 控制弹窗显示与隐藏
      dialogVisible: false,
      // 弹窗标题
      dialogTitle: '',
      // 弹窗 库区名称
      dialogName: '',
      dialogId: ''
    }
  },
  created() {
    this.pageQueryWareHouse(this.params)
  },
  methods: {
    // 获取全部仓库
    async pageQueryWareHouse(val) {
      const { data } = await pageQueryWareHouse(val)
      // console.log(data)
      this.tableData = data.records
      this.total = data.total
    },
    handleSizeChange(val) {
      this.params.size = val
      this.pageQueryWareHouse(this.params)
    },
    handleCurrentChange(newPage) {
      this.params.current = newPage
      this.pageQueryWareHouse(this.params)
    },
    // 搜索
    searchFn() {
      this.pageQueryWareHouse({ ...this.params, ...this.inputData })
    },
    // 重置
    resetFn() {
      this.params = {
        size: 10,
        current: 1
      }
      this.inputData = {}
      this.pageQueryWareHouse(this.params)
    },
    // 点击 新增按钮
    async addWareHouse() {
      const { data } = await getNextCode('CK')
      console.log(data)
      this.nextCode = data
      this.$router.push({
        name: 'Details',
        params: { code: this.nextCode }
      })
    },
    // 点击 编辑按钮
    async edit(row) {
      console.log(row)
      this.$router.push({
        name: 'Details',
        params: { row: row, id: row.id }
      })
      await searchWareHouse(row.id)
    },
    // 点击 删除按钮
    delBtn(row) {
      this.$confirm('确定永久删除该仓库？', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteWareHouse(row.id)
        this.$message.success('删除成功！')
        this.pageQueryWareHouse(this.params)
      }).catch(() => {
        this.$message.info('已取消删除！')
      })
    },
    // 停用 开启
    switchBtn(row) {
      console.log(row)
      this.dialogVisible = true
      this.dialogTitle = this.statusMap[Number(!row.status)]
      this.dialogName = row.name
      this.dialogStatus = row.status
      this.dialogId = row.id
    },
    // 弹窗确定
    async submit() {
      // this.dialogStatus = !this.dialogStatus
      await changeWareHouse({ id: this.dialogId, status: Number(!this.dialogStatus) })
      this.$message.success('修改状态成功！')
      this.dialogVisible = false
      // this.tableData.status = !this.tableData.status
      this.pageQueryWareHouse(this.params)
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 25px;
}
.top-card {
  margin-bottom: 20px;
  span {
    color: #b5abab;
    font-size: 12px;
    display: block;
    margin-bottom: 10px;
  }
  .el-button {
    border-radius: 20px;
    margin-top: 25px;
  }
  .btn1 {
    background-color: #ffb200;
    color: black;
  }
  .btn1:hover {
    color: black;
    background-color: #ff8e00;
  }
}
</style>
